const getApps = getApp().globalData
const util = require('../../utils/util.js')
const rate = wx.getSystemInfoSync().windowWidth / 750
const Monitor = require('../../utils/monitor.js').Monitor
// ********************************************************************************
Page(Monitor.hookApp({
  data: {
    swiperItem: [0, 1, 2, 3, 4],
    list: [],
    rate,
    loading: false,
    pageNum: 1,
    showNum: 3,
    length: 4,
    index: 0,
    kongImage: false,
    tarbar: [{
        status: 99,
        image: getApps.imgUrl + "2e833201807261550333010.png",
        name: "全部",
      },
      {
        status: 0,
        image: getApps.imgUrl + "2e833201807261550333010.png",
        name: "待付款"
      },
      {
        status: 1,
        image: getApps.imgUrl + "2e833201807261550333010.png",
        name: "待发货"
      },
      {
        status: 3,
        image: getApps.imgUrl + "2e833201807261550333010.png",
        name: "待收货"
      },
      {
        status: 2,
        image: getApps.imgUrl + "2e833201807261550333010.png",
        name: "已取消"
      },
    ],
    imgUrls: getApp().globalData.imgUrls
  },
  jump(e) {
    if (e.currentTarget.dataset.class == 'pay') {
      wx.navigateTo({
        url: `../Dingdan/Dingdan?order_id=${e.currentTarget.dataset.orderid}`,
      })
    }
  },
  onLoad(options) {
    getApp().getPagesurl('my')
    this.setData({
      status: options.status
    })
  },
  onShow() {
    this.data.pageNum = 1
    this.data.list = []
    this.left()
  },
  bindscrolltolower() {
    this.data.pageNum++
      this.orderList('Noempty')
  },
  tarbarClick(e) {
   

    var status = e.currentTarget.dataset.item.status
    if (this.data.index != e.currentTarget.dataset.index) {
      this.setData({
        list: [],
        pageNum: 1,
        status,
        index: e.currentTarget.dataset.index,
        kongImage: false
      })
    }

  },
  left() {
    var that = this
    this.data.tarbar.some((item, index) => {
      if (item.status == this.data.status) {
        that.setData({
          index
        })
        that.orderList()
      }
    })
  },
  bindchangeS(e) {
    var index = e.detail.current
    this.setData({
      list: [],
      pageNum: 1,
      index,
    })
    this.data.status = this.data.tarbar[index].status
    this.orderList()
  },
  orderList(Noempty) {
    var that = this
    util.API('pyapi/user_order_list', {
      status: that.data.status,
      page: that.data.pageNum,
      pageshow: 3,
    }, 'application/x-www-form-urlencoded').then(res => {

      let {
        code,
        orders_list,
        msg
      } = res
      if (code == 200) {
        orders_list.map((item, index) => {
          let totleNum = 0
          item.goods_list.map((items) => {
            totleNum += items.nums
          })
          item.totleNum = totleNum;
          // 如果是充值订单
          if (item.order_type === 5) {
            item.date = util.getTimeDian(item.ctime)
          }
        })

        if (!Noempty) {
          that.data.list = []
        }

        that.data.list = that.data.list.concat(orders_list);
        this.setData({
          kongImage: false,
          list: that.data.list
        })
      } else {
        if (this.data.list.length == 0) {
          that.setData({
            kongImage: true
          })
        }
        that.setData({
          loading: true,
        })
      }

    })
  },
  getLogistics(e) { //点击查看物流
    wx.navigateTo({
      url: `../Logistics/Logistics?order_id=${e.currentTarget.dataset.id}`,
    })
  },
  //点击取消订单
  cancellationOfOrder(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: e.currentTarget.dataset.status == 'shanchu' ? '是否确定删除订单' : '是否确定取消订单',
      success(res) {
        if (res.confirm) {
          util.API('pyapi/Cancellation_order', {
            order_id: e.currentTarget.dataset.id
          }, 'application/x-www-form-urlencoded').then(res => {
            that.orderList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  receipt(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success(res) {
        console.log(res)
        if (res.confirm) {
          util.API('pyapi/Confirmation_receipt', {
            order_id: e.currentTarget.dataset.orderid
          }, 'application/x-www-form-urlencoded').then(res => {
            wx.showToast({
              title: '收货成功',
              duration: 1000
            })
            setTimeout(() => {
              that.setData({
                list: [],
                page: 1
              })
              that.orderList()
            }, 1000)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //  储值订单再次购买
  goChuZhi(e){ 
    let { need_pay, order_id} = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/ChongZhi/ChongZhi?order_id=${order_id}&need_pay=${need_pay}`,
    })
  },  
  gopay(e) {
    if (e.currentTarget.dataset.status == 'pinglun') {
      wx.navigateTo({
        url: `../Shaidan/Shaidan?order_id=${e.currentTarget.dataset.orderid}`,
      })
    }
  },
  refund() { // 退款
    wx.navigateTo({
      url: '/pages/DingDanTuiKuan/DingDanTuiKuan',
    })
  },
  jumpXQ(e) { // 点击再次购买
    console.log(e)
    let goods = e.currentTarget.dataset.goods
    goods = goods.map((item) => {
      let obj = {
        "goods_id": item.goods_id,
        "goods_stock": 1,
        "sku": 215,
        "uniqueKey": "68775d26b16d43bab128"
      }
      return obj
    })
    // wx.navigateTo({
    //   url: `../DingdanPay/DingdanPay?goods=${e.currentTarget.dataset.goodsid}`,
    // })
  },
  jumpDdxq(e) { //点击查看订单详情
    let distributiontype = e.currentTarget.dataset.distributiontype,
      status = e.currentTarget.dataset.status,
      str = `order_id=${e.currentTarget.dataset.orderid}&distributiontype=${distributiontype}`
    if (status == 2) { // 订单已经取消
      wx.navigateTo({
        url: `../DingDanXiangQingQuXiao/DingDanXiangQingQuXiao?${str}`,
      })
    } else {

      if (distributiontype == 1) { //商家配送
        if (status == 1) { // 商家配送--待发货
          wx.navigateTo({
            url: `../DingDanXiangDaiFaHuo/DingDanXiangDaiFaHuo?${str}`
          })
        } else if (status == 3) { // 同城限时达--待收货
          wx.navigateTo({
            url: `../DingDanXiangDaiShouHuo/DingDanXiangDaiShouHuo?${str}`
          })
        } else if (status == 5) { // 订单完成
          wx.navigateTo({
            url: `../DingDanXiangYiWanChengSJPS/DingDanXiangYiWanChengSJPS?${str}`
          })
        }
      } else if (distributiontype == 2) { // 同城限时达
        if (status == 1) { // 同城限时达--待发货
          wx.navigateTo({
            url: `../DingDanXiangDaiFaHuo/DingDanXiangDaiFaHuo?${str}`
          })
        } else if (status == 3) { // 同城限时达--待收货
          wx.navigateTo({
            url: `../DingDanXiangDaiShouHuo/DingDanXiangDaiShouHuo?${str}`
          })
        } else if (status == 5) { // 订单完成
          wx.navigateTo({
            url: `../DingDanXiangYiWanChengSJPS/DingDanXiangYiWanChengSJPS?${str}`
          })
        }
      } else if (distributiontype == 3) { // 到店自提
        if (status == 1) { // 到店自提--待发货
          var order_id =
            wx.navigateTo({
              url: `../DingDanXiangBeiHuo/DingDanXiangBeiHuo?${str}`
            })
        }
        if (status == 3) { // 到店自提--待收货
          wx.navigateTo({
            url: `../DingDanXiangYiWanCheng/DingDanXiangYiWanCheng?${str}&heXiao=false`
          })
        }
        if (status == 5) { // 到店自提--完成
          wx.navigateTo({
            url: `../DingDanXiangYiWanCheng/DingDanXiangYiWanCheng?${str}&heXiao=true`
          })
        }
      }
    }
  }
}))